﻿layui.define(['jquery'], function (exports) {
    var $ = layui.$;
    var layer = layui.layer;
    var host = location.host; //获取主机名+端口号 例如：172.20.11.111:8000
    var obj = {
        httpAsync: function (url, type, data, callback) {
            $.ajax({
                type: type,
                url: `http://${host}/${url}`,
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json',
                timeout: 3000,
                cache: false,
                async: true,
                beforeSend: function () { },
                success: function (data) {
                    if (data.code == 200) {
                        callback(data.result);
                    } else {
                        layer.alert(data.message)
                    }
                },
                error: function (XMLHttpRequest, errMsg, errThrown) { //请求失败调用
                    layer.alert(errMsg)
                }
            });
        }
    };
    //输出接口
    exports('http', obj);
});